package tech.aistar.service.order.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.aistar.entity.Demand;
import tech.aistar.entity.DemandOrder;
import tech.aistar.entity.UserInfo;
import tech.aistar.mapper.center.UserInfoMapper;
import tech.aistar.mapper.deamnd.DemandMapper;
import tech.aistar.mapper.order.DemandOrderMapper;
import tech.aistar.service.order.IDemandOrderService;
import tech.aistar.util.MakeOrderNum;
import tech.aistar.vo.DemandOrderVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 23827 on 2019/9/28.
 */
@Service
public class DemandOrderServiceImpl implements IDemandOrderService {

    @Autowired
    private DemandOrderMapper demandOrderMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private DemandMapper demandMapper;

    @Override
    public List<DemandOrder> findAll() {
        List<DemandOrder> list = demandOrderMapper.findAll();
        return list;
    }

    @Override
    public List<DemandOrder> getById(Integer id) {
        List<DemandOrder> list3 = demandOrderMapper.getById(id);
        return list3;
    }

    @Override
    public List<DemandOrder> find(String order_number) {
        List<DemandOrder> list2 = demandOrderMapper.find(order_number);
        return list2;
    }

    @Override
    public DemandOrder delById(Integer id) {
        demandOrderMapper.delById(id);
        return null;
    }


    @Override
    public DemandOrder insertDemandOrder(DemandOrder demandOrder) {
        String orderNumber = MakeOrderNum.makeOrderNum();
        demandOrder.setOrderNumber(orderNumber);
        demandOrderMapper.insertDemandOrder(demandOrder);

        return demandOrder;
    }

    @Override
    public void updateDemandOrder(DemandOrder demandOrder) {
        demandOrderMapper.updateDemandOrder(demandOrder);

    }



    @Override
    public List<DemandOrder> getByStatus(Integer status) {
        List<DemandOrder> list4 = demandOrderMapper.getByStstus(status);
        return list4;
    }

    //第三方实体类
    @Override
    public List<DemandOrderVo> demandOrderVo() {
        List<Demand> demandList = demandMapper.findAll();
        List<UserInfo> userInfoList = userInfoMapper.findAll();
        List<DemandOrder> demandOrderList = demandOrderMapper.findAll();
        List<DemandOrderVo> demandOrderVoList = new ArrayList<>();
        for (int i = 0; i <demandOrderList.size() ; i++) {
            DemandOrderVo de = new DemandOrderVo();
            DemandOrder dem = demandOrderList.get(i);
            UserInfo u = userInfoList.get(i);
            Demand d = demandList.get(i);
            de.setDemand(d);
            //获取需求订单表中的id
            Integer id = dem.getId();
            de.setId(id);
            //获取需求表中的intro
            String intro = d.getIntro();
            de.setIntro(intro);
            //获取需求表的price
            Float price = d.getPrice();
            de.setPrice(price);
            //获取用户详细表中的名称
            String name = u.getName();
            de.setName(name);
            //获取需求订单表的时间
            Date CommitTime = dem.getCommitTime();
            de.setCommitTime(CommitTime);
            //获取需求订单表的编号
            String orderNumber = dem.getOrderNumber();
            de.setOrderNumber(orderNumber);
            //获取需求订单表中的状态
            Integer status = dem.getStatus();
            de.setStatus(status);
            demandOrderVoList.add(de);
        }

        return demandOrderVoList;
    }

//    @Override
//    public DemandOrder delGetById(Map<Integer, Object> updateIdMap) {
//        Map<Integer,Object> resultMap = new HashedMap();
//        return null;
//    }

//    @Override
//    public DemandOrder delGetById(Integer id) {
////        demandOrderMapper.delById(id);
//        List<DemandOrderVo> demandOrderVoList = new ArrayList<>();
//        for (int i = 0; i <demandOrderVoList.size() ; i++) {
//            DemandOrderVo de = new DemandOrderVo();
//            DemandOrder d =demandOrderMapper.delById(id);
//            Integer did = d.getId();
//            de.setId(did);
//            Integer status = d.getStatus();
//            de.setStatus(status);
//            demandOrderVoList.add(de);
//        }
//
//        return null;
//    }




    @Override
    public PageInfo<DemandOrder> findAll(String order_number, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<DemandOrder> phoneList = demandOrderMapper.find(order_number);

        PageInfo<DemandOrder> pageInfo = new PageInfo<>(phoneList);

        return pageInfo;
    }
}
